package cn.qf.shop.controller;

import cn.qf.shop.pojo.entity.Assets;
import cn.qf.shop.pojo.qo.AssetsQo;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.AssetsManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.List;

@EnableSwagger2
@RequestMapping("/assets")
@Api(tags = "资产管理业务接口")
@RestController
@RestControllerAdvice
public class AssetsController extends BaseController{
    private Logger logger = LoggerFactory.getLogger(AssetsController.class);
    @Resource
    private AssetsManagementService assetsManagementService;

    /**
     * 查询所有资产信息列表，返回给前端页面
     * @return
     */

    @ResponseBody
    @ApiOperation("资产管理数据展示")
    @PostMapping(value = "/showManagement",produces = {"application/json;charset=UTF-8"})
    public RespResult showManagement(){
        List<Assets> assets = assetsManagementService.selectAll();
        RespResult respResult=null;
        if (assets==null){
            respResult = super.returnRespWithFailed("找不到用户列表");
        }
        respResult= super.returnRespWithSuccess(assets);
        return respResult;
    }

    /**
     * 根据搜索框搜索用户信息
     * @param assetsQo
     * @return
     */
    @ResponseBody
    @ApiOperation("资产动态查询")
    @RequestMapping(value = "/selectManagement",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult selectManagement(AssetsQo assetsQo){
        List<Assets> assets = assetsManagementService.selectByKeyWord(assetsQo);
        RespResult respResult=null;
        if (assets==null){
            respResult = super.returnRespWithFailed("找不到资产列表");
        }
        respResult= super.returnRespWithSuccess(assets);
        return respResult;
    }

    /**
     * 根据用户勾选的id进行批量删除
     * @param assetsIds
     * @return
     */
    @ResponseBody
    @ApiOperation("批量删除")
    @RequestMapping(value = "/deleteManagement",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult deleteManagement(@RequestParam(value = "assetsIds[]") List<Integer> assetsIds){
        int i = assetsManagementService.updateByAssetsIds(assetsIds);
        RespResult respResult=null;
        if (i==-1){
            respResult = super.returnRespWithFailed("删除失败");
        }
        respResult=showManagement();

        return respResult;
    }

    /**
     * 添加数据
     * @param assetsQo
     * @return
     */
    @ResponseBody
    @ApiOperation("增加数据")
    @RequestMapping(value = "/AddManagement",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult AddManagement(AssetsQo assetsQo){
        int i = assetsManagementService.insertAssets(assetsQo);
        RespResult respResult=null;
        if (i==-1){
            respResult = super.returnRespWithFailed("添加失败");
        }
        respResult=showManagement();
        return respResult;
    }

    /**
     * 根据点击鼠标来修改数据
     * @param assetsQo
     * @return
     */
    @ResponseBody
    @ApiOperation("修改数据")
    @RequestMapping(value = "/updateByOnclick",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult updateByOnclick(AssetsQo assetsQo){
        int i = assetsManagementService.updateByOnclick(assetsQo);
        RespResult respResult=null;
        if (i==-1){
            respResult.setMessage("修改失败");
        }
        respResult=showManagement();

        return respResult;
    }
    /**
     * 根据点击图标删除所在的行
     * @param assetsQo
     * @return
     */
    @ResponseBody
    @ApiOperation("删除一行")
    @RequestMapping(value = "/deleteByid",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult deleteByid(AssetsQo assetsQo){
        int i = assetsManagementService.updateByAssetsId(assetsQo);
        RespResult respResult=null;
        if (i==-1){
            respResult.setMessage("删除失败");
        }
        respResult=showManagement();
        return respResult;
    }
}
